package pl.edu.agh.gronoMining.frequentSubgraphFinder.graph.simpleGraph;

import java.util.Comparator;

import pl.edu.agh.gronoMining.frequentSubgraphFinder.graph.Edge;
import pl.edu.agh.gronoMining.frequentSubgraphFinder.graph.GraphException;
import pl.edu.agh.gronoMining.frequentSubgraphFinder.graph.Node;

public class SimpleEdge implements Edge {

	private int label;
	private Node nodeA;
	private Node nodeB;
	
	public SimpleEdge(int label, Node nodeA, Node nodeB) {
		this.label = label;
		this.nodeA = nodeA;
		this.nodeB = nodeB;
	}
	
	public int getLabel() {
		return label;
	}
	
	public void checkIntegraty() throws GraphException {
		this.nodeA.addEdge(this);
		this.nodeB.addEdge(this);
	}
	
	public Node getNodeA() {
		return nodeA;
	}
	
	public Node getNodeB() {
		return nodeB;
	}
	
	public Node getOtherNode(Node node) {
		if (node.equals(nodeA))
			return nodeB;
		else if (node.equals(nodeB))
			return nodeA;
		else
			return null;
	}
	
	public void sortNodes(Comparator<Node> c) {
		if (c.compare(this.nodeA, this.nodeB) < 0) {
			Node tmpNode = this.nodeA;
			this.nodeA = this.nodeB;
			this.nodeB = tmpNode;
		}
	}
	
	public boolean equals(Object o) {
		Edge e = (Edge) o;
		if (e.getLabel() != this.label)
			return false;
		if (! e.getNodeA().equals(this.nodeA))
			return false;
		if (! e.getNodeB().equals(this.nodeB))
			return false;
		return true;
	}
	
	public int hashCode() {
		return this.nodeA.hashCode() + this.nodeB.hashCode();
	}
	
	public String toString() {
		return "Edge: " + this.nodeA.getId() +", " + this.getNodeA().getType() + ":" + this.getNodeA().getLabel() +
			" - " + this.nodeB.getId() + ", " + this.getNodeB().getType() + ":" + this.getNodeB().getLabel();
	}
}
